<?php

/**
 * buscar actions.
 *
 * @package    PhpProject1
 * @subpackage buscar
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class buscarActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $decEdificio = new DecEdificio();
        $decEdificioTable = $decEdificio->getTable()->findAll();
        $this->decEdificioTable = $decEdificioTable;

        $decHabitacionTipo = new DecHabitacionTipo();
        $decHabitacionTipoTable = $decHabitacionTipo->getTable()->findAll();
        $this->decHabitacionTipoTable = $decHabitacionTipoTable;
    }

    public function executeBuscar(sfWebRequest $request) {
        $inputEdificio = $request->getParameter("inputEdificio");
        $inputTipo = $request->getParameter("inputTipo");
        $inputDesde = $request->getParameter("inputDesde");
        $inputHasta = $request->getParameter("inputHasta");

        $ini = date("Y-m-d", strtotime($inputDesde));
        $fin = date("Y-m-d", strtotime($inputHasta));
        $array = array();
        $aux = $ini;
        while ($fin >= $aux) {
            array_push($array, "'" . $aux . "'");
            $aux = date("Y-m-d", strtotime($aux . " + 1 days"));
        }
        $implode = implode(",", $array);

        $connection = Doctrine_Manager::connection();
        $query = "
            SELECT GROUP_CONCAT(DISTINCT reb.reb_habitacion_id) AS reb_habitacion_id 
            FROM dec_reservacion res 
            INNER JOIN dec_reservacion_habitacion reb ON res.res_id = reb.reb_reservacion_id 
            WHERE (res.res_estado IN('RESERVADO','CONFIRMADO')) AND (res.res_fecha_entrada IN($implode) OR res.res_fecha_salida IN($implode))";
        $statement = $connection->execute($query);
        $statement->execute();
        $resultset = $statement->fetchObject();
        $reb_habitacion_id = $resultset->reb_habitacion_id;
        $habIdArray = split(",", $reb_habitacion_id);

        $whereEdificio = $inputEdificio == "0" ? "hab.hab_edificio_id > 0" : "hab.hab_edificio_id = $inputEdificio";
        $whereTipo = $inputTipo == "0" ? "hab.hab_habitacion_tipo_id > 0" : "hab.hab_habitacion_tipo_id = $inputTipo";

        if (!is_null($reb_habitacion_id)) {
            $decHabitacionTable = Doctrine_Query::create()
                    ->select("hab.*")
                    ->from("DecHabitacion hab")
                    ->whereNotIn("hab.hab_id", $habIdArray)
                    ->andWhere($whereEdificio)
                    ->andWhere($whereTipo)
                    ->execute();
        } else {
            $decHabitacionTable = Doctrine_Query::create()
                    ->select("hab.*")
                    ->from("DecHabitacion hab")
                    ->where($whereEdificio)
                    ->andWhere($whereTipo)
                    ->execute();
        }

        $this->decHabitacionTable = $decHabitacionTable;
    }

}
